package mop.gui.resource;

import java.util.LinkedList;
import javax.swing.JButton;
import javax.swing.table.TableCellRenderer;
import mop.controller.CreateResourceController;
import mop.gui.GUI;
import mop.gui.component.JTableButtonModel;
import mop.gui.component.JTableButtonMouseListener;
import mop.gui.component.JTableButtonRenderer;
import mop.gui.main.MainPanelInterface;
import mop.model.resource.ResourceInterface;

/**
 * List of projects.
 *
 * @author  MOP Team 7
 */
public class ResourcePanel extends javax.swing.JPanel implements MainPanelInterface
{
    /**
     * Constructor.
     *
     * @param   gui
     * @param   removeProjectController
     */
    public ResourcePanel(GUI gui, CreateResourceController createResourceController)
    {
        initComponents();
        this.setGui(gui);
        this.createResourceController = createResourceController;
        tableResources.addMouseListener(new JTableButtonMouseListener(tableResources));
    }

    /***************************************************************************
     *                          GUI reference
     ***************************************************************************/

    private GUI gui;

    /**
     * Sets the GUI.
     *
     * @param   gui
     * @post    gui is set.
     */
    private void setGui(GUI gui)
    {
        this.gui = gui;
    }

    /**
     * Returns the GUI reference.
     *
     * @return  this.gui
     */
    public GUI getGui()
    {
        return this.gui;
    }

    /***************************************************************************
     *                             resources
     ***************************************************************************/

    private CreateResourceController createResourceController;

    /**
     * Loads resources in resources table.
     */
    public void update()
    {
        LinkedList<ResourceInterface> resources = new LinkedList<ResourceInterface>();
        resources = (LinkedList<ResourceInterface>) createResourceController.getAllResources();

        JTableButtonModel tableModel = new JTableButtonModel();

        String[] columns = {"Resource Description", "Resource Type"};
        Object[][] rows = generateResourceRows(resources);

        tableModel.setColumns(columns);
        tableModel.setRows(rows);

        tableResources.setModel(tableModel);
        TableCellRenderer defaultRenderer = tableResources.getDefaultRenderer(JButton.class);
        JTableButtonRenderer buttonRenderer = new JTableButtonRenderer(defaultRenderer);
        tableResources.setDefaultRenderer(JButton.class, buttonRenderer);
    }

    /**
     * Generates the rows of the resources table.
     *
     * @param   resources
     *          List of resource descriptions.
     * @return  The rows for the table.
     */
    private Object[][] generateResourceRows(LinkedList<ResourceInterface> resources)
    {
        Object[][] rows = new Object[resources.size()][2];

        for ( int i = 0 ; i < resources.size() ; i++ ) {

            rows[i][0] = resources.get(i);

            rows[i][1] = resources.get(i).getResourceType().getId();

        }

        return rows;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblResources = new javax.swing.JLabel();
        projectsPane = new javax.swing.JScrollPane();
        tableResources = new javax.swing.JTable();
        btnCreateResource = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(517, 569));

        lblResources.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        lblResources.setText("Resources"); // NOI18N

        tableResources.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        projectsPane.setViewportView(tableResources);

        btnCreateResource.setText("Create Resource"); // NOI18N
        btnCreateResource.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateResourceActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(projectsPane, javax.swing.GroupLayout.DEFAULT_SIZE, 497, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(lblResources)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 290, Short.MAX_VALUE)
                        .addComponent(btnCreateResource)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCreateResource)
                    .addComponent(lblResources))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(projectsPane, javax.swing.GroupLayout.DEFAULT_SIZE, 515, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnCreateResourceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateResourceActionPerformed
        gui.showCreateResourceDialog();
}//GEN-LAST:event_btnCreateResourceActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCreateResource;
    private javax.swing.JLabel lblResources;
    private javax.swing.JScrollPane projectsPane;
    private javax.swing.JTable tableResources;
    // End of variables declaration//GEN-END:variables

}
